package io.itit.ecp.admin.server.service.impl;

import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import io.itit.ecp.admin.common.enums.IMeterDeviceEnums;
import io.itit.ecp.admin.server.entity.*;
import io.itit.ecp.admin.server.mapper.SceneProjectMapper;
import io.itit.ecp.admin.server.service.*;
import io.itit.grass.common.exception.GrassException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：项目管理服务实现类
 * @Date：Create by 2023-11-23 11:46:13
 */
@Service
public class SceneProjectServiceImpl extends JoinServiceImpl<SceneProjectMapper, SceneProjectEntity> implements ISceneProjectService {


    @Resource
    private ISceneEdificeService sceneEdificeService;
    @Resource
    private ISceneRoomService sceneRoomService;
    @Resource
    private ISceneCompanyService companyService;
    @Resource
    private IMeterDeviceService meterDeviceService;
    @Resource
    private IDeviceService deviceService;
    @Resource
    private IEarlyWarningService earlyWarningService;
    @Resource
    private IEnergyDataService energyDataService;
    @Resource
    private ICarbonSinkDataService carbonSinkDataService;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeProjects(List<String> ids) {
        // 查询关联的楼栋.
        long countEd = sceneEdificeService.lambdaQuery()
                .select(SceneEdificeEntity::getId)
                .in(SceneEdificeEntity::getSceneProjectId,ids)
                .count();
        GrassException.throwEx(countEd > 0,"项目已被楼栋模块关联，请在解绑后重试！");

        // 查询关联的楼层
        long countRo = sceneRoomService.lambdaQuery()
                .select(SceneRoomEntity::getId)
                .in(SceneRoomEntity::getSceneProjectId,ids)
                .count();
        GrassException.throwEx(countRo > 0,"项目已被空间模块关联，请在解绑后重试！");

        // 查询关联的公司
        long countCo = companyService.lambdaQuery()
                .select(SceneCompanyEntity::getId)
                .in(SceneCompanyEntity::getSceneProjectId,ids)
                .count();
        GrassException.throwEx(countCo > 0,"项目已被公司模块关联，请在解绑后重试！");

        // 查询关联的计量设备
        long countMd = meterDeviceService.lambdaQuery()
                .select(MeterDeviceEntity::getId)
                .in(MeterDeviceEntity::getSceneProjectId,ids)
                .eq(MeterDeviceEntity::getDelFlag, IMeterDeviceEnums.DelFlag.delFlag1.delFlag)
                .count();
        GrassException.throwEx(countMd > 0,"项目已被计量设备模块关联，请在解绑后重试！");

        // 查询关联的设备
        long countDevice = deviceService.lambdaQuery()
                .select(DeviceEntity::getId)
                .in(DeviceEntity::getSceneProjectId,ids)
                .count();
        GrassException.throwEx(countDevice > 0,"项目已被设备模块关联，请在解绑后重试！");

        // 查询关联的预警设置
        long countEarlyWarning = earlyWarningService.lambdaQuery()
                .select(EarlyWarningEntity::getId)
                .in(EarlyWarningEntity::getSceneProjectId,ids)
                .count();
        GrassException.throwEx(countEarlyWarning > 0,"项目已被预警设置模块关联，请在解绑后重试！");

        // 查询关联的能源数据
        long countEnergyData = energyDataService.lambdaQuery()
                .select(EnergyDataEntity::getId)
                .in(EnergyDataEntity::getSceneProjectId,ids)
                .count();
        GrassException.throwEx(countEnergyData > 0,"项目已被能源数据模块关联，请在解绑后重试！");

        // 查询关联的碳汇数据
        long countCarbonSinkData = carbonSinkDataService.lambdaQuery()
                .select(CarbonSinkDataEntity::getId)
                .in(CarbonSinkDataEntity::getSceneProjectId,ids)
                .count();
        GrassException.throwEx(countCarbonSinkData > 0,"项目已被碳汇数据模块关联，请在解绑后重试！");

        return this.removeBatchByIds(ids,1000);
//        List<SceneEdificeEntity> edifices = sceneEdificeService.lambdaQuery()
//                .select(SceneEdificeEntity::getId)
//                .in(SceneEdificeEntity::getSceneProjectId, ids)
//                .list();
//        if (!edifices.isEmpty()){
//            List<String> edificeIds = edifices.stream().map(SceneEdificeEntity::getId).collect(Collectors.toList());
//            sceneEdificeService.removeEdifices(edificeIds);
//        }
//        return this.removeBatchByIds(ids, 1000);
    }

    @Override
    public String getProjectIdByName(String projectName) {
        SceneProjectEntity project = this.lambdaQuery()
                .select(SceneProjectEntity::getId)
                .eq(SceneProjectEntity::getProjectName, projectName)
                .one();
        return project == null ? null : project.getId();
    }


}
